﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using transportsystem.DTO;
using transportsystem.BUS;
namespace transportsystem
{
    public partial class frmTuyen : Form
    {        
        DataTable dt;
        bool hideControls = true;
        int iIndex = 0;
        bool addNew = false;
        string imPath = "";
        public frmTuyen()
        {
            InitializeComponent();
        }
        void HideAddNew(bool value)
        {
            if (value == true)
                pnlAddNew.Visible = false;
            else
                pnlAddNew.Visible = true;
        }

        private void chkHideControls_CheckedChanged(object sender, EventArgs e)
        {
            hideControls = !hideControls;
            HideAddNew(hideControls);
        }
        public void LoadData()
        {
            HideAddNew(hideControls);
            dt = TuyenBUS.layDanhSachTuyen();
            if (dt != null && dt.Rows.Count > 0)
            {
                dgv.AutoGenerateColumns = false;
                dgv.DataSource = dt;

            }
            iIndex = 0;
            //SetValueToControls(iIndex);
            DanhSachLichTrinh();
        }
        private void frmTuyen_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        public void SetValueToControls(int iIndex)
        {            
            if (dt.Rows.Count > 0)
            {
                
                int ma = Convert.ToInt32(dt.Rows[iIndex]["matuyen"]);
                DataTable t = TuyenBUS.layDanhSachTuyenTheoMa(ma);

                txtMaTuyen.Text = t.Rows[0]["matuyen"].ToString();
                txtTenTuyen.Text = t.Rows[0]["tentuyen"].ToString();
                txtNoiDi.Text = t.Rows[0]["noidi"].ToString();
                txtNoiDen.Text = t.Rows[0]["noiden"].ToString();
                txtDoDai.Text = t.Rows[0]["dodai"].ToString();
                cbLichTrinh.SelectedValue = t.Rows[0]["malichtrinh"].ToString();

                int z = int.Parse(t.Rows[0]["trangthai"].ToString());
                if (z == 0)
                    chkTrangThai.Checked = false;
                else
                    chkTrangThai.Checked = true;

                txtHeSoGiaVe.Text = t.Rows[0]["hesogiave"].ToString();
            }
        }

        private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls(iIndex);
            txtCapNhatHeSo.Text = "";
        }
        public void DanhSachLichTrinh()
        {
            DataTable dtlichtrinh = LichTrinhBUS.layDanhSachLichTrinh();
            cbLichTrinh.DataSource = dtlichtrinh;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            chkHideControls.Checked = true;
            hideControls = !chkHideControls.Checked;
            HideAddNew(hideControls);
            AddNewPrepair();
        }

        private void AddNewPrepair()
        {
            
            txtMaTuyen.Text = "";
            txtTenTuyen.Text = "";
            txtNoiDen.Text = "";
            txtNoiDi.Text = "";
            txtDoDai.Text = "";
            chkTrangThai.Checked = false;
            chkTrangThai.Text = "";
            cbLichTrinh.SelectedItem = 0;
            txtMoTa.Text = "";
            txtCapNhatHeSo.Visible = false;
            btCapNhatHeSo.Visible = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }
        private void Save()
        {
            Tuyen tuyen = new Tuyen();
            if (txtMaTuyen.Text != "")
                tuyen.Matuyen = Convert.ToInt32(txtMaTuyen.Text);
            tuyen.Tentuyen = txtTenTuyen.Text;
            tuyen.Noidi = txtNoiDi.Text;
            tuyen.Noiden = txtNoiDen.Text;
            tuyen.Dodai = txtDoDai.Text;
            if (chkTrangThai.Checked == true)
                tuyen.Trangthai = 1;
            else
                tuyen.Trangthai = 0;
            tuyen.Hesogiave = Convert.ToInt32(txtHeSoGiaVe.Text);
            tuyen.Malichtrinh = (int)cbLichTrinh.SelectedValue;
            tuyen.Mota = txtMoTa.Text;
            Boolean kq = TuyenBUS.capNhatTuyen(tuyen);
            if (kq == true)
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Có lỗi xảy ra! Vui lòng thử lại", "Thông báo", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            LoadData();
            addNew = false;
        }

        private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            addNew = false;
            iIndex = e.RowIndex;
            SetValueToControls(iIndex);
            txtCapNhatHeSo.Text = "";
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (addNew == false)
            {
                Tuyen tuyen = new Tuyen();
                tuyen.Matuyen = int.Parse(txtMaTuyen.Text.Trim());
                TuyenBUS.xoaTuyen(tuyen);
                LoadData();
            }
        }

        private void btSearch_Click(object sender, EventArgs e)
        {
            String tentuyen = txtTimKiem.Text;            
            dt = TuyenBUS.traCuuTuyen(tentuyen);
            dgv.DataSource = dt;
            

        }

        private void btCapNhatHeSo_Click(object sender, EventArgs e)
        {
            Tuyen t = new Tuyen();
            t.Matuyen = int.Parse(txtMaTuyen.Text);
            t.Hesogiave = float.Parse(txtCapNhatHeSo.Text);

            bool kq = TuyenBUS.capNhatHeSoGiaVe(t);
            if (kq == true)
                MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Có lỗi xảy ra! Vui lòng thử lại", "Thông báo", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            LoadData();
            txtCapNhatHeSo.Text = "";
        }
        

                
    }
}
